/*
 * @Author: your name
 * @Date: 2020-02-22 11:50:25
 * @LastEditTime: 2020-02-23 00:22:57
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /dytext/src/router/index.ts
 */
import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
import { setPageTitle, getToken,setTimeDate } from "@/utils";
const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [
    {
      path: "",
      name: "login",
      component: () => import("@/views/Login.vue"),
      meta: {
        title: "登录"
      }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import("@/views/Register.vue"),
      meta: {
        title: '注册'
      }
    },
    {
      path: "/logout",
      name: "logout",
      component: () => import("@/views/Logout.vue"),
      meta: {
        title: "退出登录"
      }
    },
    {
      path: "/home",
      name: "home",
      component: () => import("@/views/Home.vue"),
      meta: {
        title: "东阳市疫情防控检查表"
      }
    }
  ]
});
router.beforeEach((to, from, next) => {
  if (!getToken()) {
    if (to.name !== "login" && to.name !== 'register') {
      next({ path: "" });
    } else {
      next();
    }
  } else {
    if(to.name === 'login' || to.name === 'register') {
      next({
        path:'/home'
      })
    }else {
      next()
    }
  }
  if (to.meta && to.meta.title) {
    setPageTitle(to.meta.title);
  }
  next();
});

export default router;
